package com.example.day_blog.controller;
import com.example.day_blog.model.Result;
import com.example.day_blog.model.UserInfo;
import com.example.day_blog.service.UserService;
import com.example.day_blog.utils.IdentifyCodeUtils;
import com.example.day_blog.utils.JwtUtils;
import com.example.day_blog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 18212
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(String username, String password, String identifyCode, HttpSession session){

        //判读输入是否合法
        if(!StringUtils.hasLength(username) ||!StringUtils.hasLength(password) ||! StringUtils.hasLength(identifyCode))
        {

            return Result.fial("输入不合法");
        }
        //通过用户名查找用户信息
        UserInfo userInfo=userService.queryByName(username);
        //从session中取出验证码
        String sessionCode = (String)session.getAttribute("identifyCode");
        System.out.println("从session中获取的验证码"+session.getAttribute("identifyCode"));
        //开始校验
        String pp=SecurityUtils.encry(password);
        System.out.println(pp);
        if (userInfo.getId()<0) {
            return Result.fial("用户不存在");

        }
        //盐值加密
        else if(SecurityUtils.verify(password,userInfo.getPassword())&& identifyCode.equals(sessionCode))//identifyCode.equalsIgnoreCase(sessionCode))
             {
                 Map<String,Object>claim=new HashMap<>();
            claim.put("id",userInfo.getId());
            claim.put("name",userInfo.getUserName());
            String token= JwtUtils.getToken(claim);
            return Result.success(token);
        }
        else {
            System.out.println("验证码错误");
            return Result.fial("验证未通过");
        }
    }
    //获取用户信息
    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request)
    {
        //从token中获取
        String token=request.getHeader("user_token");
        System.out.println("token = " + token);
        Integer id=JwtUtils.getUserIdFromToken(token);
        System.out.println("id = " + id);
        if(id==null)
        {
            return null;
        }
        UserInfo userInfo=userService.queryById(id);
        userInfo.setAtricals(userService.articals(id));
        return userInfo;
    }
    //获取当前博客作者信息
    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId)
    {
        if(blogId==null ||blogId<0)
        {
            return null;
        }

        //通过blogid获取userid
        UserInfo userInfo=userService.getAuthorInfo(blogId);
        userInfo.setAtricals(userService.articals(userInfo.getId()));
        return userInfo;
    }
  @RequestMapping("regist")
  public Result regist(String username, String password, String Github){

      //判读输入是否合法
      if(!StringUtils.hasLength(username) ||!StringUtils.hasLength(password) ||! StringUtils.hasLength(Github))
      {

          return Result.fial("输入不合法",false);
      }
      //通过用户名查找用户信息
      UserInfo userInfo=userService.queryByName(username);
      if (userInfo!=null) {
          return Result.fial("用户以存在，无需注册",false);
      }
      else
      {
          UserInfo userInfo1=new UserInfo();
          userInfo1.setUserName(username);
          userInfo1.setPassword(SecurityUtils.encry(password));
          userInfo1.setGithubUrl(Github);
          userService.insertUser(userInfo1);
          return Result.success(true);
      }
  }

     /** 给前端返回一个验证码图片
     * @return
     */
    @RequestMapping("/identifyImage")
    public void identifyImage(HttpServletResponse response, HttpSession session){
        //创建随机验证码
        IdentifyCodeUtils utils = new IdentifyCodeUtils();
        String identifyCode = (String) utils.getIdentifyCode();
        //session存入验证码
        session.setAttribute("identifyCode", identifyCode);
        System.out.println("存入的session中的验证码："+identifyCode);
        //根据验证码创建图片
        BufferedImage identifyImage = utils.getIdentifyImage(identifyCode);
        //回传给前端
        utils.responseIdentifyImg(identifyImage,response);
    }
}


